#include "KeyInput.h"

#include "gpio.h"

#define GPIOGroup GPIOB
#define ZeroKey GPIO_PIN_12
#define ExcessKey GPIO_PIN_13

static uint8_t KeyStatus = 0;
static uint8_t KeyStatus_last = 0;

void GetKeyCallback(void)
{
    uint8_t data = 0;
    KeyStatus_last = KeyStatus;
    KeyStatus = 0; // 状态清零
    data = (uint8_t)HAL_GPIO_ReadPin(GPIOGroup, ExcessKey);
    KeyStatus = (KeyStatus << 1) | data;
    data = (uint8_t)HAL_GPIO_ReadPin(GPIOGroup, ZeroKey);
    KeyStatus = (KeyStatus << 1) | data;
}

uint8_t GetKeyState(uint8_t num)
{
    return (KeyStatus >> num) & 0x01;
}

uint8_t GetKeyStateAll(void)
{
    return KeyStatus;
}

uint8_t GetEdgeDown(uint8_t num)
{
    return ((KeyStatus ^ KeyStatus_last & KeyStatus_last) >> num) & 0x01;
}
